package com.falcon.mojo;

import com.falcon.util.PathUtil;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.File;
import java.io.IOException;

/** 清空合并的源码文件 */
@Mojo(name = "source-clean", defaultPhase = LifecyclePhase.CLEAN)
public class SourceCleanMojo extends AbsMojo {
    public void execute() throws MojoExecutionException, MojoFailureException {
        String modulePath = this.project.getBasedir().getAbsolutePath();

        getLog().info("[源码清理] 清理目录 src/main/java ");
        this.cleanDir(PathUtil.getJavaSourceDir(modulePath));

        getLog().info("[源码清理] 清理目录 src/main/resource ");
        this.cleanDir(PathUtil.getResourcesDir(modulePath));

        getLog().info("[源码清理] 清理目录 src/test/java ");
        this.cleanDir(PathUtil.getTestJavaSourceDir(modulePath));

        getLog().info("[源码清理] 清理目录 src/test/resource ");
        this.cleanDir(PathUtil.getTestResourcesDir(modulePath));
    }

    private void cleanDir(String filePath) {
        File directory = new File(filePath);
        if (directory.exists() && directory.isDirectory()) {
            try {
                FileUtils.cleanDirectory(directory);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
